package org.kjtc.service;

import org.kjtc.entity.Sprint;
import org.kjtc.entity.SprintCommon;
import org.kjtc.entity.WorkState;
import org.kjtc.exception.UpdateRuntimeException;
import org.kjtc.mapper.SprintMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/12/4 0004.
 */
@Service
public class SprintService {

    @Autowired
    private SprintMapper sprintMapper;

    public List<Sprint> getSprint(String ProductVersionID) {
        List<Sprint> lists = sprintMapper.getSprint(ProductVersionID);
        List<SprintCommon> listDone = new ArrayList<>();
        for(Sprint s:lists){
            listDone = sprintMapper.getDoneWorkBySprintId(s.getSprintID());
            for(SprintCommon sc:listDone){
                s.setDoneWork(sc.getDoneWork());
                s.setAllWork(sc.getAllWork());
            }
        }
        return lists;
    }

    public List<SprintCommon> getSprintBySprintId(String sprintID) {
        List<SprintCommon> listDone = sprintMapper.getDoneWorkBySprintId(sprintID);
        List<SprintCommon> listDate = sprintMapper.getSprintDateByID(sprintID);
        for(SprintCommon ld:listDate){
            for(SprintCommon sc:listDone){
                ld.setComplete((int)(Double.valueOf(sc.getDoneWork())/Double.valueOf(sc.getAllWork())*100)+"%");
            }
        }
        return listDate;

    }
    public List<SprintCommon> getTaskState(String sprintID) {
        List<SprintCommon> listDone = sprintMapper.getDoneWorkBySprintId(sprintID);
        return listDone;
    }
    public int addSprint(Sprint sprint){
        sprint.setSprintID(new SimpleDateFormat("yyyymmddhhmmss").format(new Date()));
        int num=sprintMapper.addSprint(sprint);
        if(num!=1){
            throw new RuntimeException("添加Sprint失败");
        }
        return num;
    }
    public List<Map<String, Object>> getBackLogStateBySprintId(String sprintID) {
        List<Map<String, Object>> lists = sprintMapper.getBackLogStateBySprintId(sprintID);
        return lists;
    }
    public List<Map<String, Object>> getBackLogCountByEveryDay(String sprintID) {
        List<Map<String, Object>> lists = sprintMapper.getBackLogCountByEveryDay(sprintID);
        return lists;
    }

    public static int differentDaysByMillisecond(Date date1,Date date2){
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }
    public List<Sprint> getSprintMessageBySprintID(String sprintID) {
        List<Sprint> lists = sprintMapper.getSprintMessageBySprintID(sprintID);
        return lists;
    }
    public int updateSprintBySprintID(Sprint sprint){
        int num=sprintMapper.updateSprintBySprintID(sprint);
        if(num!=1){
            throw new UpdateRuntimeException("修改Sprint失败");
        }
        return num;
    }
    public int deleteSprintById(String sprintID){
        int num=sprintMapper.deleteSprintById(sprintID);
       if(num!=1){
            throw new RuntimeException("删除Sprint失败");
        }
        return num;
    }
    public List<Sprint> updateCurrentTitle(String sprintID) {
        List<Sprint> lists = sprintMapper.updateCurrentTitle(sprintID);
        return lists;
    }
    public List<Sprint> getProductNameByProductVersionID(String ProductVersionID) {
        List<Sprint> list = sprintMapper.getProductNameByProductVersionID(ProductVersionID);
        return list;
    }

}

